package presale_house.show.HouseList;

import presale_house.handler.HouseViewHandler;
import presale_house.handler.MainViewHandler;
import presale_house.req.ClientRequest;
import presale_house.req.HouseRequest;
import presale_house.res.TableDTO;
import presale_house.service.HouseService;
import presale_house.service.impl.ClientServiceImpl;
import presale_house.service.impl.HouseServiceImpl;
import presale_house.show.MainTableView.MainViewTable;
import presale_house.show.MainTableView.MainViewTableModel;
import presale_house.show.MainView;
import presale_house.util.DimensionUtil;

import javax.swing.*;
import java.awt.*;

/**
 * 该类用于完成主界面显示,即对表的操作
 */
public class HouseView extends JFrame {
    //北部
    JPanel northPanel=new JPanel();
    JButton addBtn=new JButton("添加");

    JButton updateBtn=new JButton("修改");
    JButton deleteBtn=new JButton("删除");
    JTextField searchText=new JTextField(15);
    JButton searchBtn=new JButton("搜索");
    //中部
    JPanel centerPanel=new JPanel();
    MainViewTable mainViewTable=new MainViewTable();
    HouseViewTable houseViewTable=new HouseViewTable();
    //南部
    JPanel southPanel=new JPanel();
    JButton preBtn=new JButton("上一页");
    JButton nextBtn=new JButton("下一页");

    //表
    private  int pageNow=1;//当前页数
    private   int pageSize=10;//每页最多能显示10行
    private int pageStart=1;//分页查询时的起始位置
    JButton returnBtn=new JButton("返回主菜单");



    HouseViewHandler houseViewHandler;


    public HouseView(){
        super("房屋列表");
        Container container=getContentPane();
        houseViewHandler =new HouseViewHandler(this);
        Rectangle bounds=DimensionUtil.getBounds();
        pageSize=Math.floorDiv(bounds.height,35);
        //添加组件(北)
        layoutNorth(container);
        //设置中间table
        layoutCenter(container);
        //(南)
        layoutSouth(container);


        //JFrame设置
        //根据屏幕大小设置主界面大小
        setBounds(bounds);

        //设置屏幕大小
        //  setBounds(DimensionUtil.getBounds());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(null);

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);

        }

    private void layoutCenter(Container container) {
        //数据
        TableDTO tableDTO=getTableDTO();
        HouseViewTableModel houseViewTableModel=HouseViewTableModel.assembleModel(tableDTO.getData());

       houseViewTable.setModel(houseViewTableModel);
        houseViewTable.RenderRule();
        JScrollPane jScrollPane = new JScrollPane(houseViewTable);
       container.add(jScrollPane,BorderLayout.CENTER);
       //上一页/下一页判断
        shoePreNext(tableDTO.getTotalCount());
    }

    private TableDTO getTableDTO() {
        HouseServiceImpl houseService=new HouseServiceImpl();
        HouseRequest houseRequest =new HouseRequest();
        houseRequest.setPageNow(pageNow);
        houseRequest.setPageSize(pageSize);
        houseRequest.setSearchKey(searchText.getText().trim());
        TableDTO tableDTO=houseService.retrieveHouse(houseRequest);
        return tableDTO;
    }

    private void layoutSouth(Container container) {
        preBtn.addActionListener(houseViewHandler);
        nextBtn.addActionListener(houseViewHandler);
        southPanel.add(preBtn);
        southPanel.add(nextBtn);
        container.add(southPanel,BorderLayout.SOUTH);
    }

    private void layoutNorth(Container container) {
        //添加监听事件
        returnBtn.addActionListener(houseViewHandler);
        addBtn.addActionListener(houseViewHandler);
        updateBtn.addActionListener(houseViewHandler);
        deleteBtn.addActionListener(houseViewHandler);
        searchBtn.addActionListener(houseViewHandler);

        northPanel.add(returnBtn);
        northPanel.add(addBtn);
        northPanel.add(deleteBtn);
        northPanel.add(searchBtn);
        northPanel.add(updateBtn);
        northPanel.add(searchText);
        container.add(northPanel,BorderLayout.NORTH);
    }

    //判断是否显示上一页和下一页
    private void shoePreNext(int totalCount){
        if(pageNow==1){
            preBtn.setVisible(false);
        }else{
            preBtn.setVisible(true);
        }
        //求总共有多少页
        int pageCount=0;
        if(totalCount%pageSize==0){
            pageCount=totalCount/pageSize;
        }else {
            pageCount=totalCount/pageSize+1;
        }
        if(pageNow==pageCount){
            nextBtn.setVisible(false);
        }else{
            nextBtn.setVisible(true);
        }
    }

    //更新表的数据
    public void reloadTable(){
        TableDTO tableDTO=getTableDTO();
        HouseViewTableModel.updateModel(tableDTO.getData());
        houseViewTable.RenderRule();
        shoePreNext(tableDTO.getTotalCount());
    }


    public int[] getSelectedHouseIds(){
        int []selectedRows=houseViewTable.getSelectedRows();
        int[]ids=new int[selectedRows.length];
        for(int i=0;i<selectedRows.length;i++){
            int rowIndex=selectedRows[i];
            Object idObj=houseViewTable.getValueAt(rowIndex,0);
            ids[i]=Integer.valueOf(idObj.toString());
        }
        return ids;
    }

    public static void main(String[] args) {
        new HouseView();
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

}
